package cardgame;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: 86187
 * Date: 2022-07-25
 * Time: 13:53
 */

class Card {
    public int rank;  //牌的数字
    public String suit;  //牌的花色

    public Card(int rank, String suit) {  //构造方法
        this.rank = rank;
        this.suit = suit;
    }

    @Override
    public String toString() {
        return ""+suit+""+rank;
    }
}

public class CardDemo {

    public static final String[] suits = {"♥","♠","♣","♦"}; //花色

    //买一副牌
    public List<Card> buyDestCard() {

        List<Card> cards = new ArrayList<>();

        for (int i = 1; i <= 13; i++) {
                    //每个花色有13张牌
            for (int j = 0; j < 4; j++) {
                    //每个牌有4中花色

                Card card = new Card(i,suits[j]); //”买“到一张牌
                cards.add(card);  //将牌放入顺序表cards中
            }
        }
        return cards;
    }

    //洗牌
    public void shuffleCard(List<Card> cards) {
        for (int i = cards.size()-1; i > 0 ; i--) {
            Random random = new Random();
            int index = random.nextInt(i);
             swap(cards,index,i);
        }
    }
    /**
     * 洗牌的具体操作
     * @param cards 一副牌，洗牌的对象
     * @param i 交换的两张牌之一
     * @param j 交换的两张牌之一
     */
    private void swap(List<Card> cards, int i, int j) {
        Card tmp = cards.get(i);
        cards.set(i,cards.get(j));
        cards.set(j,tmp);
    }

    //玩牌：三个人，每个人轮流抓5张牌
    public List<List<Card>> test(List<Card> cards) {
        //3个人
        List<Card> hand1 = new ArrayList<>();
        List<Card> hand2 = new ArrayList<>();
        List<Card> hand3 = new ArrayList<>();

        List<List<Card>> hands = new ArrayList<List<Card>>();
        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);

        for (int i = 0; i < 5; i++) {
            for (int j = 0; j < 3; j++) {
                //cards顺序表中删除的牌就是三个人要接的牌
                 Card card = cards.remove(0);
                 hands.get(j).add(card);
            }
        }
        return hands;
    }
}
